package mx.jmcprograms.uploadindex.util;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FTPClientMgr {
	
	public FTPClientMgr() {
		
	}
	public FTPClientMgr(String user,String password,String server){
		this.host=server;
		this.user=user;
		this.password=password;
		
	}

	private String host;
	private String user;
	private String password;
	
	public void sendFile2(String nomFileStored,String folder) throws IOException{
		FTPClient ftp = new FTPClient( );
		
		ftp.connect( host );     
		int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            ftp.disconnect();
            System.err.println("FTP server refused connection.");
            //System.exit(1);
        }
        ftp.login(user, password);
        ftp.changeWorkingDirectory(folder);
        boolean deleteFile1 = ftp.deleteFile("index.html");
        System.out.println("resultado ftp delete: "+deleteFile1);
        FileInputStream file=new FileInputStream(nomFileStored);
        boolean storeFile = ftp.storeFile(nomFileStored, file);
        System.out.println("resultado ftp: "+storeFile);
        file.close();
        ftp.disconnect();
		
	    /*
	    // Connect to an FTP server on port 21.
	    
			ftp.connect(host, 21, user, password);
			// Set binary mode.
		    ftp.bin();
		    
		    // Change to a new working directory on the FTP server.
		    ftp.cwd(folder);
		    
		    // Upload some files.
		    ftp.stor(new File(nomFileStored));
		    // Quit from the FTP server.
		    ftp.disconnect();
		    */
		
		
	}
	public void sendFile(String nomFileStored,String folder) throws IOException{
		
		String cadenaParams="-b -s "+this.host+" "+this.user+" "+this.password+" "+folder+" "+nomFileStored;
		FTPClientExample client=new FTPClientExample();
		client.main(cadenaParams.split(" "));
	}
	

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
